package org.xiaoxin.onecoupon.engine.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xiaoxin.onecoupon.engine.dto.req.CouponCreatePaymentReqDTO;
import org.xiaoxin.onecoupon.engine.dto.req.CouponProcessPaymentReqDTO;
import org.xiaoxin.onecoupon.engine.dto.req.CouponProcessRefundReqDTO;
import org.xiaoxin.onecoupon.engine.dto.req.CouponTemplateRedeemReqDTO;
import org.xiaoxin.onecoupon.engine.service.UserConponService;
import org.xiaoxin.onecoupon.framework.result.Result;
import org.xiaoxin.onecoupon.framework.web.Results;

/**
 * @author xiaoxin
 * 2025/5/12
 */

@RestController
@RequiredArgsConstructor
@Slf4j
@Tag(name ="用户优惠劵管理")
public class UserCouponController {
   private  final UserConponService userConponService;
   @Operation(summary = "兑换优惠券模板", description = "存在较高流量场景，可类比“秒杀”业务")
   @RequestMapping("/api/engine/user-coupon/redeem")
   public Result<Void> redeemUserConpon(@RequestBody CouponTemplateRedeemReqDTO requestParam){
      userConponService.redeemUserConpon(requestParam);
      return Results.success();
   }
   @Operation(summary = "兑换优惠券模板之消息队列", description = "存在较高流量场景，可类比“秒杀”业务")
   @PostMapping("/api/engine/user-coupon/redeem-mq")
   public Result<Void> redeemUserCouponByMQ(@RequestBody CouponTemplateRedeemReqDTO requestParam) {
      userConponService.redeemUserCouponByMQ(requestParam);
      return Results.success();
   }
   @Operation(summary = "创建用户优惠券结算单", description = "用户下单时锁定使用的优惠券，一般由订单系统发起调用")
   @PostMapping("/api/engine/user-coupon/create-payment-record")
   public  Result<Void> createPaymentRecord(@RequestBody CouponCreatePaymentReqDTO couponCreatePaymentReqDTO){
      userConponService.createPaymentRecord(couponCreatePaymentReqDTO);
      return Results.success();
   }
   @Operation(summary = "核销用户优惠券结算单", description = "用户支付后核销使用的优惠券，常规来说应该监听支付后的消息队列事件")
   @PostMapping("/api/engine/user-coupon/process-payment")
   public Result<Void> processPayment(@RequestBody CouponProcessPaymentReqDTO couponProcessPaymentReqDTO){
      userConponService.processPayment(couponProcessPaymentReqDTO);
      return  Results.success();
   }
   @Operation(summary = "退款优惠券结算单", description = "用户退款成功后返回使用的优惠券，常规来说应该监听退款成功后的消息队列事件")
   @PostMapping("/api/engine/user-coupon/process-refund")
   public Result<Void> processRefund(@RequestBody CouponProcessRefundReqDTO requestParam) {
      userConponService.processRefund(requestParam);
      return Results.success();
   }
}
